<template>
  <div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      :error.sync="error"
       error-text="请求失败，点击重新加载"
    >
      <van-cell v-for="(item,index) in resultList" :key="index" :title="item.title" />
    </van-list>
  </div>
</template>

<script>
import { getSearchResultsAPI } from '@/api/search.js'
export default {
  data () {
    return {
      resultList: [],
      loading: false,
      finished: false,
      page: 1,
      // perPage: 14
      error: false
    }
  },
  props: {
    searchText: {
      type: String,
      required: true
    }
  },
  methods: {
    async onLoad () {
      // this.getSearchResultsFn()
      try {
        // 发送请求
        const { results } = await getSearchResultsAPI({
          page: this.page, // 页码
          per_page: 10, // 每页大小
          q: this.searchText // 搜索关键字
        })
        if (Math.random() > 0.3) {
          console.lo(1)
        }
        this.page++
        if (!results.length) {
          this.finished = true
          return
        }
        // 保存数据
        this.resultList.push(...results)
        // console.log(this.resultList)
        // 关闭loading
        this.loading = false
      } catch (error) {
        this.loading = false
        this.error = true
      }
    }
  }
  // created () {
  //   this.getSearchResultsFn()
  // }
}
</script>

<style>

</style>
